import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../models/order_list_model.dart';
import '../manger/cached_image_widget.dart';
import '../order/controller/order_pay.dart';
import '../../class/controller/product_detail.dart';
import '../../manger/qysdk_manger.dart';
class OrderItemWidget extends StatelessWidget {
  final OrderArrayModel order;
  final AppLocalizations localizations;
  final String Function(String status, AppLocalizations localizations) getStatusText;
  final VoidCallback? onContentTap;
  final VoidCallback? copyIdTap;

  const OrderItemWidget({
    super.key,
    required this.order,
    required this.localizations,
    required this.getStatusText,
    this.copyIdTap,
    this.onContentTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 15, bottom: 0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        children: [
          _buildOrderHeader(context),
          Divider(
            height: 1,
            thickness: 1,
            indent: 10,
            endIndent: 10,
            color: Color(0xFFF6F6F6),
          ),
          _buildOrderContent(context),
          Divider(
            height: 1,
            thickness: 1,
            indent: 10,
            endIndent: 10,
            color: Color(0xFFF6F6F6),
          ),
          _buildOrderFooter(context),
        ],
      ),
    );
  }

  Widget _buildOrderHeader(BuildContext context) {
    return Container(
      height: 40,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        children: [
          Text(
            '${AppLocalizations.of(context)!.orderNumber}:${order.orderId}',
            style: TextStyle(
              color: Color(0xFF666666),
              fontSize: 12,
              letterSpacing: -0.5,
            ),
          ),
          SizedBox(width: 6),
          GestureDetector(
            onTap: () {
              if (copyIdTap != null) {
                copyIdTap!();
              }
            },
            child: Container(
              height: 20,
              width: 39,
              decoration: BoxDecoration(
                color: Color(0xFFF4F4F4),
                borderRadius: BorderRadius.circular(10),
              ),
              alignment: Alignment.center,
              child: Text(
                localizations.copy,
                style: TextStyle(
                  color: Color(0xFF444444),
                  fontSize: 12,
                ),
              ),
            ),
          ),
          SizedBox(width: 6),
          Expanded(
            child: Text(
              getStatusText(order.status, localizations),
              textAlign: TextAlign.right,
              style: TextStyle(
                color: Color(0xFF666666),
                fontSize: 13,
              ),
            ),
          ),
        ],
      ),
    );
  }

Widget _buildOrderContent(BuildContext context) {
  return GestureDetector(
    onTap: () {
      if (onContentTap != null) {
        onContentTap!();
      }
    },
    child: Container(
      height: 100,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 15),
      color: Colors.transparent,
      child: Stack(
        clipBehavior: Clip.none,  // 防止子组件溢出
        children: [
          Row(
            children: [
              CachedImage(
                imageUrl: order.pic,
                width: 66,
                height: 66,
                fit: BoxFit.cover,
              ),
              SizedBox(width: 20),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: MediaQuery.of(context).size.width - 150,
                    child: Text(
                      order.spuName,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 15,
                      ),
                    ),
                  ),
                  Text(
                    localizations.type + '：' + order.attrvalue,
                    style: TextStyle(
                      color: Color(0xFF666666),
                      fontSize: 12,
                    ),
                  ),
                ],
              ),
            ],
          ),
          // 在图片上方添加绝对定位的卡密图片和文字
          if (order.rechargeType == 2)
          Positioned(
            top: -6,
            left: -15,
            child: Image.asset(
              'assets/images/cardsInfo.png',
              width: 95,
              height: 46,
              fit: BoxFit.contain,
            ),
          ),
          if (order.rechargeType == 2)
          Positioned(
            top: 2,
            left: 0,
            child: Text(
              localizations.cardInfo,
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    ),
  );
}


  Widget _buildOrderFooter(BuildContext context) {
    return Container(
      height: 45,
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            localizations.total + order.symbol + order.paymentAmount.toString(),
            style: TextStyle(
              color: Color(0xFF333333),
              fontSize: 14,
            ),
          ),
          _buildActionButtons(context),
        ],
      ),
    );
  }

  Widget _buildActionButtons(BuildContext context) {
    if (order.status == '0') {
      return SizedBox(
        height: 30,
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => OrderPayPage(orderId: order.orderId, spuId: order.spuId),
              ),
            );
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: Color(0xFF5D5FEE),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
              side: BorderSide(color: Color(0xFF5D5FEE), width: 1),
            ),
            padding: EdgeInsets.symmetric(horizontal: 8),
          ),
          child: Text(
            localizations.goPay,
            style: TextStyle(color: Colors.white, fontSize: 14),
          ),
        ),
      );
    } else if (order.status == '1') {
      return Row(
        children: [
          SizedBox(
            height: 30,
            child: ElevatedButton(
              onPressed: () {
                QYSDK.showQYSession();             
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                  side: BorderSide(color: Color(0xFF5D5FEE), width: 1),
                ),
                padding: EdgeInsets.symmetric(horizontal: 8),
              ),
              child: Text(
                isChineseLocale() ? localizations.shipment : localizations.shipment2,
                style: TextStyle(color: Color(0xFF5D5FEE), fontSize: 14),
              ),
            ),
          ),
          SizedBox(width: 8),
          if (isChineseLocale())
          SizedBox(
            height: 30,
            child: ElevatedButton(
              onPressed: () {
                QYSDK.showQYSession();
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                  side: BorderSide(color: Color(0xFF5D5FEE), width: 1),
                ),
                padding: EdgeInsets.symmetric(horizontal: 8),
              ),
              child: Text(
                localizations.contactCustomer,
                style: TextStyle(color: Color(0xFF5D5FEE), fontSize: 14),
              ),
            ),
          ),
        ],
      );
    } else if (order.status == '3' || order.status == '8' || order.status == '9' || order.status == '5') {
      return SizedBox(
        height: 30,
        child: ElevatedButton(
          onPressed: () {
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) => ProductDetailPage(spuId: order.spuId),
              ),
              (route) => route.isFirst,
            );         
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 6),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
              side: BorderSide(color: Color(0xFF5D5FEE), width: 1),
            ),
          ),
          child: Text(
            localizations.orderAgain,
            style: TextStyle(color: Color(0xFF5D5FEE), fontSize: 14),
          ),
        ),
      );
    }
    return SizedBox.shrink();
  }
}